package com.tcl.aota.admin.manager;

import com.tcl.aota.admin.dto.FileDTO;
import com.tcl.aota.admin.job.StoreFilePool;
import com.tcl.aota.admin.util.FileStoreUtil;
import com.tcl.aota.common.constants.Constants;
import com.tcl.aota.common.utils.AmazonS3Op;
import com.tcl.aota.common.utils.FileUtil;
import com.tcl.aota.common.utils.JsonParser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @author kelong
 * @date 11/6/14
 */
@Component("amazons3Manager")
public class Amazons3Manager {
    private static Logger LOG = Logger.getLogger(Amazons3Manager.class);
    @Resource
    private AmazonS3Op amazons3Op;


    /**
     * copy file to S3
     *
     * @param fileDTO
     */
    public void copy(FileDTO fileDTO) {
        int i = 0;
        boolean flag = false;
        String filePath = fileDTO.getFilePath();
        File file = new File(filePath);
        if (!file.exists()) {
            return;
        }
        do {
            flag = amazons3Op.copy(file, fileDTO.getDescPath());
            i++;
            if (!flag) {
                LOG.error("s3 failed,try count:" + i + "========" + fileDTO.getDescPath());
            }
        } while (!flag && i < Constants.AMAZON.AGAIN_COUNT);

        if (!flag) {
            //warn TODO
            //文件移动到上传失败目录，等待job继续上传
            String descCopyPath = Constants.Common.FAILURE_FILE_DIR + file.getName();
            File descFile = new File(descCopyPath);
            FileStoreUtil.copyFile(file, descFile);
            fileDTO.setFilePath(descCopyPath);
            String logJson = JsonParser.toString(fileDTO);
            String logPath = Constants.Common.FAILURE_FILE_PATH;
            FileUtil.writeToFileByLock(logPath, logJson);
            LOG.error("user store file to s3 failure through 3 times." + logJson);
        } else {
            //删除本地文件
            int delFlag = fileDTO.getDelFlag();
            if (file.exists() && delFlag == 1) {
                file.delete();
            }
        }
    }


    /**
     * delete file from amazon s3
     *
     * @param path
     */
    public void deleteFile(String path) {
        int i = 0;
        boolean flag = false;
        do {
            flag = amazons3Op.deleteFile(path);
            i++;
        } while (!flag && i < Constants.AMAZON.AGAIN_COUNT);

        if (!flag) {
            //warn TODO
            //相对路径
            String logPath = Constants.Common.FAILURE_FILE_PATH;
            FileDTO fileDTO = new FileDTO();
            fileDTO.setFilePath("");
            fileDTO.setFlag(Constants.AMAZON.DELETE_FLAG);
            fileDTO.setDescPath(path);
            String logJson = JsonParser.toString(fileDTO);
            FileUtil.writeToFileByLock(logPath, logJson);
            LOG.error("user delete file from s3 failure through 3 times." + path);
        }
    }

    /**
     * add one file to sync queue
     *
     * @param file
     * @param descPath
     */
    public void syncStoreFile(File file, String descPath) {
        FileDTO fileDTO = new FileDTO();
        fileDTO.setFlag(Constants.AMAZON.UPLOAD_FLAG);
        fileDTO.setFilePath(file.getPath());
        fileDTO.setDescPath(descPath);
        StoreFilePool.getThreadPool().add(fileDTO);
    }

    /**
     * add store files to sync queue
     *
     * @param fileMaps
     * @return
     */
    public void syncStoreFiles(Map<File, String> fileMaps) {
        for (Map.Entry<File, String> entry : fileMaps.entrySet()) {
            File file = entry.getKey();
            String descPath = entry.getValue();
            FileDTO fileDTO = new FileDTO();
            fileDTO.setFlag(Constants.AMAZON.UPLOAD_FLAG);
            fileDTO.setFilePath(file.getPath());
            fileDTO.setDescPath(descPath);
            StoreFilePool.getThreadPool().add(fileDTO);
        }
    }

    /**
     * add del file to sync queue
     *
     * @param filePaths
     */
    public void syncDelFiles(List<String> filePaths) {
        for (String path : filePaths) {
            FileDTO fileDTO = new FileDTO();
            fileDTO.setFlag(Constants.AMAZON.DELETE_FLAG);
            fileDTO.setDescPath(path);
            StoreFilePool.getThreadPool().add(fileDTO);
        }
    }


}
